<?php
namespace App\Services\Recommend\Recommend;

use App\Models\Recommend\Recommend;
use Illuminate\Support\Facades\DB;
use App\Services\Manage\ManageIndex;

/**
 * @Author: 蒋哲
 * @Date:   2017-05-27 16:25:06
 * @Last Modified by:   ws
 * @Last Modified time: 2018-06-12 
 */
class RecommendLogic
{
    use \App\Services\Goods\Traits\UpdateCate;
    use \App\Services\User\Traits\addAppId;
    use \App\Services\Goods\Traits\GetCateTree;
    use \App\Services\Goods\Traits\OperateAppCate;
    /*检验识别码的唯一性*/
    public function checkAction($data): array
    {
        $info = Recommend::where(['action'=>$data['action']])->first();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

    /*内部获取某个分类的详情*/
    public function dbCateInfo($data)
    {
        return Recommend::where(['id'=>$data['id']])->where('status','<>',-1)->first();
    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map = [])
    {
        isset($map['id']) && $_where[] = ['id','=',$map['id']];
        isset($map['pid']) && $_where[] = ['pid','=',$map['pid']];
        isset($map['app_id']) && $_where[] = ['app_id','=',$map['app_id']];
        $_where[] = ['status','<>',-1];
        return Recommend::where($_where)->orderBy('order','ASC')->orderBy('id','DESC')->get()->toArray();
    }


    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
    public function dbAllCateList($map = [])
    {
        isset($map['id']) && $_where[] = ['id', '=', $map['id']];
        isset($map['pid']) && $_where[] = ['pid', '=', $map['pid']];
        if (isset($map['status'])) {
            $_where[] = ['status', '=', $map['status']];
        } else {
            $_where[] = ['status', '<>', -1];
        }
        $que = Recommend::where($_where);
        if (isset($map['app_id'])) {   //若卖家查看商品详情，则不传入app_id
        $manageAppId = ManageIndex::Api()->getAppId();
        $app_id = $map['app_id'];

        if ($manageAppId != $map['app_id']) {
            $que->Where(function ($query) use ($app_id) {
                $query->where('app_id', $app_id)->orWhere(function ($query) use ($app_id) {
                    $query->whereRaw("FIND_IN_SET($app_id,`other_app_read`)");
                })->orWhere('is_public', 1);
            });
        }
    }

        if (isset($map['limit']) && $map['limit']) {
            $que->limit($map['limit'])
                ->orderBy('order', 'ASC')
                ->orderBy('id', 'ASC');
        } else {
            $que->orderBy('order', 'ASC')
                ->orderBy('id', 'ASC');
        }

        $res = $que->get()->toArray();
        return $res;
    }

    //添加推荐位分类
    public function addRecommend($data): array
    {
        unset($data['token']);
        $res = Recommend::insert($data);
        return auto_return($res, '添加分类成功', '添加分类失败');
    }
    
    /*编辑某个推荐位*/
    public function editRecommend($data): array
    {
        return $this->_updateCate('\App\Services\Recommend\Recommend\RecommendLogic', '\App\Models\Recommend\Recommend', $data);
    }
    /*后台获取本应用下的分类树 包含公共分类 包含除“删除”外的所有分类*/
    public function getTree($data): array
    {
        return $this->_getCateTree($data, '\App\Services\Recommend\Recommend\RecommendLogic', 'dbAllCateList');
    }

    /*获取某个不删除的分类详情*/
    public function oneCateInfo($data): array
    {
        $map[] = ['status','<>', -1];
        $Model = new \App\Models\Recommend\Recommend;

        return $this->_oneCateInfo($data, $Model, $map);
    }

    /*后台删除某个分类*/
    public function delOneCate($data): array
    {
        $Model = new \App\Models\Recommend\Recommend;
        return $this->_delOneCate($data, $Model);
    }

//    /*前台获取action所属分类下的子类 多级*/
//    public function getChildByAction($data): array
//    {
//        //获取该分类的id
//        $_where['action'] = $data['action'];
//        $_where['status'] = 1;
//        $manageAppId = \app\manage\service\ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
//        $actionInfo = $this->model($this->recommendModel)->where($_where)->where($_whereOr)->find();
//        if(!$actionInfo){
//            return null_data(null, '该分类不存在');
//        }
//        $_getChild['status'] = 1;
//        $_getChild['app_id'] = $data['app_id'];
//        $list = $this->dbAllCateList($_getChild);
//        $list = list_to_tree($list,'id','pid','_child',$actionInfo['id']);
//
//        return auto_return($list, '成功获取分类列表', '获取分类列表失败');
//    }
//
//    /*前台获取action所属分类下的子类 一级*/
//    public function getOneLevChildByAction($data): array
//    {
//        //获取该分类的id
//        $_where['action'] = $data['action'];
//        $_where['status'] = 1;
//        $manageAppId = \app\manage\service\ApiBase::getAppId();
//        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
//        $actionInfo = $this->model($this->recommendModel)->where($_where)->where($_whereOr)->find();
//        if(!$actionInfo){
//            return null_data(null, '该分类不存在');
//        }
//        $_getChild['status'] = 1;
//        $_getChild['pid'] = $actionInfo['id'];
//        $_getChild['app_id'] = $data['app_id'];
//        $_getChild['limit'] = $data['limit'];
//        $list = $this->dbAllCateList($_getChild);
//
//        return auto_return($list, '成功获取分类列表', '获取分类列表失败');
//    }

   
}